# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libvirt
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
_pkgdev=9.7.0
pkgver=${_pkgdev//-/}
pkgrel=1
pkgdesc="Windows libvirt virtualization library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://libvirt.org/'
license=('spdx:LGPL-2.1-or-later OR GPL-2.0-or-later')
depends=(${MINGW_PACKAGE_PREFIX}-curl
         ${MINGW_PACKAGE_PREFIX}-glib2
         ${MINGW_PACKAGE_PREFIX}-gnutls
         ${MINGW_PACKAGE_PREFIX}-gettext
         ${MINGW_PACKAGE_PREFIX}-libgcrypt
         ${MINGW_PACKAGE_PREFIX}-libgpg-error
         ${MINGW_PACKAGE_PREFIX}-libssh
         ${MINGW_PACKAGE_PREFIX}-libssh2
         ${MINGW_PACKAGE_PREFIX}-libxml2
         ${MINGW_PACKAGE_PREFIX}-portablexdr
         ${MINGW_PACKAGE_PREFIX}-readline
         ${MINGW_PACKAGE_PREFIX}-yajl)
makedepends=(${MINGW_PACKAGE_PREFIX}-meson
             ${MINGW_PACKAGE_PREFIX}-pkgconf
             ${MINGW_PACKAGE_PREFIX}-python
             ${MINGW_PACKAGE_PREFIX}-python-docutils
             ${MINGW_PACKAGE_PREFIX}-python-flake8
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-libxslt"
             rpcsvc-proto
             perl)
source=("https://libvirt.org/sources/${_realname}-${_pkgdev}.tar.xz"{,.asc})
sha256sums=('d8c758fe7db640f572489caa8ea6dd8262d169a4372326c23a3a013cdc40b8ce'
            'SKIP')
validpgpkeys=('453B65310595562855471199CA68BE8010084C9C') # Jiří Denemark <jdenemar@redhat.com>
noextract=(${_realname}-${_pkgdev}.tar.xz)

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  [[ -d ${srcdir}/${_realname}-${_pkgdev%-*} ]] && rm -rf ${srcdir}/${_realname}-${_pkgdev%-*}
  tar -C ${srcdir} --exclude=tests -xJf ${srcdir}/${_realname}-${_pkgdev}.tar.xz || true
}

build() {
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    --prefix="${MINGW_PREFIX}" \
    --buildtype=plain \
    -Drpath=disabled \
    -Ddriver_qemu=disabled \
    -Ddriver_openvz=disabled \
    -Ddriver_lxc=disabled \
    -Ddriver_vbox=disabled \
    -Ddriver_libvirtd=disabled \
    -Ddriver_esx=disabled \
    -Ddriver_hyperv=disabled \
    -Ddriver_vmware=disabled \
    -Dsasl=disabled \
    -Dpolkit=disabled \
    -Dnetcf=disabled \
    -Daudit=disabled \
    -Ddtrace=disabled \
    -Dtests=disabled \
    -Dexpensive_tests=disabled \
    ../${_realname}-${_pkgdev%-*}

  ${MINGW_PREFIX}/bin/meson compile
}

check() {
  cd ${srcdir}/build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/meson test || true
}

package() {
  cd ${srcdir}/build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/meson install --destdir="${pkgdir}"

  install -Dm644 ${srcdir}/${_realname}-${_pkgdev%-*}/COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 ${srcdir}/${_realname}-${_pkgdev%-*}/COPYING.LESSER "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LESSER"
}
